/*
 * ClientNotifier.cpp
 *
 *  Created on: Nov 13, 2011
 *      Author: gonzalo
 */

#include "ClientNotifier.h"
#include "../Constantes.h"

ClientNotifier::ClientNotifier(ColaMensajeRecibir* dtos,ColaMensajesSDL* sdl) {
	this->sdlReader = new ColaSDLReader(this,sdl,&this->aLlenar,CLIENT_NOTIFY_EVENT);
	this->dtoReader = new ColaMensajesReader(this,dtos,&aLlenar,CLIENT_NOTIFY_DTO);

	this->sdl = sdl;
	this->dtos = dtos;

}

void ClientNotifier::start(){
	this->running = true;

	this->dtoReader->start();
	this->sdlReader->start();
}

ClientNotifier::~ClientNotifier() {
	delete dtoReader;
	delete sdlReader;
}

void ClientNotifier::terminar(){
	this->running = false;

	this->dtoReader->terminar();
	this->sdlReader->terminar();

	this->sdl->signal();
	this->dtos->signal();

	this->dtoReader->join();
	this->sdlReader->join();

	signal();
}

void* ClientNotifier::pull(TypeNotifier* typeReturned) {

	if(!running)
		return NULL;

	if(aLlenar.empty())
		wait();

	std::pair<void*, TypeNotifier> par =this->aLlenar.pop();

	if(!par.first)
		return NULL;

	*typeReturned = par.second;
	void* retorno = par.first;


	return retorno;
}
